#ifndef ALIENGEN_IO_H
#define ALIENGEN_IO_H

#include <vector>
#include <iostream>
#include <sstream>
#include <fstream>
#include <time.h>
#include <string.h>
#include <string>
#include <algorithm>
#include <map>
#include <errno.h>
typedef long SOCKET;
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/tcp.h>

#define SOCKET_ERROR (-1)
#define closesocket( socket ) close( socket );

class AlienGenIO
{
private:
    SOCKET			_socket;
    __time_t        _timeout=30;
    unsigned int	_buffer_size = 8192;
public:
    void			setSocket(SOCKET socket);
    SOCKET			getSocket(void);
    void			setTimeout(__time_t timeout);
    void			setBufferSize(unsigned int size);
    unsigned int	getTimeout(void);
    bool			isConnected(void);

    int		sendBuffer(const char* buffer, unsigned int *size);
    char*	receiveBuffer(unsigned int* size);

    int		putInt(int value);
    int		getInt(void);
    int     getIntNew(void);

    int         putString(char* str, unsigned int* size);
    char*       getString(unsigned int *size);

};

inline
void AlienGenIO::setSocket(SOCKET socket)
{
    _socket = socket;
}

inline
SOCKET AlienGenIO::getSocket(void)
{
    return _socket;
}

inline
void AlienGenIO::setTimeout(__time_t timeout)
{
    _timeout = timeout;
}

inline
void AlienGenIO::setBufferSize(unsigned int size)
{
    _buffer_size = size;
}

inline
unsigned int AlienGenIO::getTimeout(void )
{
    return _timeout;
}

inline
bool AlienGenIO::isConnected(void )
{
    int error = 0;
    socklen_t len = sizeof (error);
    SOCKET socket_fd = getSocket();
    int retval = getsockopt (socket_fd, SOL_SOCKET, SO_ERROR, &error, &len );
    return retval == 0;
}


#endif // ALIENGEN_IO_H
